<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-row style="margin-bottom: 5px;">
        <el-col>
          <el-button
            v-if="btnPermission('menu:add:entity')"
            :disabled="addDisabled"
            icon="el-icon-circle-plus-outline"
            plain
            size="mini"
            type="primary"
            @click="add"
          >
            新增
          </el-button>
          <el-button
            v-if="btnPermission('menu:edit:entity')"
            :disabled="editDisabled"
            icon="el-icon-edit"
            plain
            size="mini"
            type="success"
            @click="edit"
          >
            编辑
          </el-button>
          <el-button
            v-if="btnPermission('menu:del:entities')"
            :disabled="delDisabled"
            icon="el-icon-circle-close"
            plain
            size="mini"
            type="danger"
            @click="del"
          >
            删除
          </el-button>
          <el-button
            v-if="btnPermission('menu:query:list')"
            icon="el-icon-refresh"
            plain
            size="mini"
            type="info"
            @click="refresh"
          >
            刷新
          </el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{textAlign: 'center'}"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        border
        row-key="id"
        style="width: 100%;"
        default-expand-all
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          align="center"
          type="selection"
          width="60"
        />
        <el-table-column
          :index="indexMethod"
          align="center"
          label="序号"
          type="index"
          width="60"
        />
        <el-table-column
          label="菜单名称"
          prop="menuName"
        />
        <el-table-column
          label="根目录"
          prop="path"
          width="90"
        />
        <el-table-column
          label="菜单路径"
          prop="menuUrl"
        />
        <el-table-column
          label="类型"
          prop="menuType"
          align="center"
          width="60"
        >
          <template slot-scope="scope">
            <span>{{ MenuType.getMenuType(scope.row.menuType) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="编码"
          prop="code"
          align="left"
        />
        <el-table-column
          label="排序"
          prop="order"
          align="center"
          width="60"
        />
        <!--        <el-table-column-->
        <!--          label="创建日期"-->
        <!--          prop="createTime"-->
        <!--          width="160"-->
        <!--        >-->
        <!--          <template slot-scope="scope">-->
        <!--            <span>{{ $moment(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作" width="260">
          <template slot-scope="scope">
            <el-button
              v-if="btnPermission('menu:add:entity')"
              plain
              size="mini"
              type="primary"
              icon="el-icon-circle-plus-outline"
              @click="handleAdd(scope.$index, scope.row)"
            >新增
            </el-button>
            <el-button
              v-if="btnPermission('menu:edit:entity')"
              plain
              size="mini"
              type="success"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              v-if="btnPermission('menu:del:entities')"
              plain
              size="mini"
              type="danger"
              icon="el-icon-circle-close"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="title" :visible.sync="dialogFormVisible" class="demo-ruleForm">
        <el-card class="box-card">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">
            <el-row v-show="ps">
              <el-col span="24">
                <el-form-item label="父菜单" prop="ParentMenuName">
                  <el-input
                    v-model="parentMenuName"
                    prefix-icon="el-icon-search"
                    placeholder="请选择父级菜单"
                    readonly
                    style="width: 590px;"
                  />
                </el-form-item>
                <el-form-item v-show="false" label="父菜单编号" prop="ParentId">
                  <el-input v-model="form.parentId"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col span="24">
                <el-form-item label="根目录" prop="Path">
                  <el-input
                    v-model="form.path"
                    :disabled="disabled"
                    prefix-icon="el-icon-search"
                    placeholder="请输入根目录"
                    style="width: 590px;"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col span="12">
                <el-form-item label="菜单名称" prop="MenuName">
                  <el-input
                    v-model="form.menuName"
                    prefix-icon="el-icon-search"
                    placeholder="请输入中文菜单名称"
                    class="colWidth"
                    auto-complete="off"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="英文菜单" prop="MenuNameEn">
                  <el-input
                    v-model="form.menuNameEn"
                    prefix-icon="el-icon-search"
                    placeholder="请输入英文菜单名称"
                    class="colWidth"
                    auto-complete="off"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="俄文菜单" prop="MenuNameRu">
                  <el-input
                    v-model="form.menuNameRu"
                    prefix-icon="el-icon-search"
                    placeholder="请输入俄文菜单名称"
                    class="colWidth"
                    auto-complete="off"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="菜单路径" prop="MenuUrl">
                  <el-input
                    v-model="form.menuUrl"
                    :disabled="disabled"
                    prefix-icon="el-icon-search"
                    placeholder="请输入菜单路径"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col span="12">
                <el-form-item label="菜单类型" prop="MenuType">
                  <el-radio-group
                    v-model="form.menuType"
                    placeholder="请输入菜单类型"
                    class="colWidth"
                    @change="change"
                  >
                    <el-radio :label="0">菜单</el-radio>
                    <el-radio :label="1">按钮</el-radio>
                    <el-radio :label="2">视图</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="图标" prop="Icon">
                  <el-input
                    v-model="form.icon"
                    :disabled="disabled"
                    placeholder="请输入图标"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col span="12">
                <el-form-item label="编码" prop="Code">
                  <el-input
                    v-model="form.code"
                    placeholder="请输入编码"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="序号" prop="Order">
                  <el-input-number
                    v-model="form.order"
                    :min="0"
                    placeholder="请输入序号"
                    label="请输入序号"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col span="24">
                <el-form-item label="备注" prop="Remark">
                  <el-input
                    v-model="form.remark"
                    type="textarea"
                    prefix-icon="el-icon-search"
                    placeholder="请输入备注"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <div slot="footer" class="dialog-footer">
          <el-button icon="el-icon-circle-check" plain size="mini" type="success" @click="submitForm">确 定</el-button>
<!--          <el-button icon="el-icon-refresh-left" plain size="mini" type="warning" @click="resetForm">重 置</el-button>-->
          <el-button icon="el-icon-close" plain size="mini" type="danger" @click="dialogFormVisible = false">取 消
          </el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import BasePage from '@/components/BasePage/BasePage'
import { Guid } from '@/constants/guid'
import { DateTime } from '@/constants/date'

export default {
  name: 'MenuManagement',
  extends: BasePage,
  data() {
    return {
      ps: true,
      disabled: false,
      parentMenuName: null,
      form: {
        id: null,
        path: null,
        menuName: null,
        menuNameEn: null,
        menuNameRu: null,
        menuUrl: null,
        parentId: null,
        order: null,
        menuType: null,
        icon: null,
        code: null,
        remark: null,
        isDel: null,
        creator: null,
        createTime: null,
        modifier: null,
        modifyTime: null
      },
      rules: {
        menuName: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          { min: 0, max: 128, message: '长度在 0 到 128 个字符', trigger: 'blur' }
        ],
        menuType: [
          { required: true, message: '请选择菜单类型', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {},
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    this.url.queryList = 'MenuManagement/Post'
    this.url.queryEntity = 'MenuManagement/GetById/'
    this.url.addEntity = 'MenuManagement/Add'
    this.url.editEntity = 'MenuManagement/Put'
    this.url.deleteEntity = 'MenuManagement/Delete/'
    this.url.deleteEntities = 'MenuManagement/DeleteRange/'
    this.queryCondition.Sort = 'Order ASC'
  },
  mounted() {
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    add: function() {
      if (this.multipleSelection === null || this.multipleSelection.length === 0) {
        this.handleAdd(null, null)
      } else {
        this.handleAdd(null, this.multipleSelection[0])
      }
    },
    edit: function() {
      this.handleEdit(null, this.multipleSelection[0])
    },
    refresh: function() {
      this.initData()
    },
    change: function() {
      if (this.form.menuType === 1 || this.form.menuType === 2) {
        this.disabled = true
      } else {
        this.disabled = false
      }
    },
    handleAdd: function(index, row) {
      this.disabled = false
      this.resetForm()
      this.title = '新增菜单信息'
      this.form.id = Guid.Empty
      if (row) {
        this.ps = true
        this.parentMenuName = row.menuName
        this.form.parentId = row.id
      } else {
        this.ps = false
      }

      this.form.path = null
      this.form.menuName = null
      this.form.menuNameEn = null
      this.form.menuNameRu = null
      this.form.menuUrl = null
      this.form.menuType = 0
      this.form.icon = null
      this.form.code = null
      this.form.order = null
      this.form.remark = null

      this.form.isDel = 0
      this.form.creator = Guid.Empty
      this.form.createTime = DateTime.Default
      this.form.modifier = Guid.Empty
      this.form.modifyTime = DateTime.Default
      this.dialogFormVisible = true
    },
    handleEdit: function(index, row) {
      this.ps = false
      this.title = '编辑菜单信息'
      Object.assign(this.form, row)
      this.dialogFormVisible = true
      this.change()
    },
    handleDelete: function(index, row) {
      this.$confirm('确定要将数据删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteEntity(row.id).then(response => {
          this.$message({ showClose: true, message: '删除成功', type: 'error' })
          this.editDisabled = true
          this.delDisabled = true
          this.initData()
        }).catch(error => {
          this.$message({ showClose: true, message: error.response.data, type: 'error' })
        })
      })
    },
    resetForm() {
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    }
  }
}
</script>

<style scoped>
.colWidth {
  width: 250px;
}
</style>
